package spring.my.db.sharding.demo.config.db;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class  SqlSessionConfig{

    @Value("${spring.mybatis.sqlSession.configLocation:''}")
    private String configLocation;
    @Value("${spring.datasource.sqlSession.typeAliasesPackage:''}")
    private String typeAliasesPackage;
    @Value("${spring.mybatis.sqlSession.mapperLocations:''}")
    private String mapperLocations;


    @Bean("dataSource")
    public DataSource buildDataSource(@Qualifier("masterDataSource") DataSource dataSource, @Qualifier("dataSource1") DataSource dataSource1, @Qualifier("dataSource2")DataSource dataSource2) {
        DynamicDataSource routing = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap();
        dataSourceMap.put("master", dataSource);
        dataSourceMap.put("slave1", dataSource1);
        dataSourceMap.put("slave2", dataSource2);
        routing.setTargetDataSources(dataSourceMap);
        routing.setDefaultTargetDataSource(dataSource);
        return routing;
    }


    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // dataSource
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        //设置mybatis configuration 扫描路径
        sqlSessionFactoryBean.setConfigLocation(pathMatchingResourcePatternResolver.getResource(this.configLocation));
        // mapper/*.xml 文件
        sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(this.mapperLocations));
        // eo 文件夹
        sqlSessionFactoryBean.setTypeAliasesPackage(this.typeAliasesPackage);
//        sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(MAPPER_PATH));
        return sqlSessionFactoryBean;
    }

//    @Bean(name = "sqlSessionFactory")
//    @Primary
//    public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("dataSource1") DataSource dataSource) throws IOException {
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        // dataSource
//        sqlSessionFactoryBean.setDataSource(dataSource);
//        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
//        //设置mybatis configuration 扫描路径
//        sqlSessionFactoryBean.setConfigLocation(pathMatchingResourcePatternResolver.getResource(this.configLocation));
//        // mapper/*.xml 文件
//        sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(this.mapperLocations));
//        // eo 文件夹
//        sqlSessionFactoryBean.setTypeAliasesPackage(this.typeAliasesPackage);
////        sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(MAPPER_PATH));
//        return sqlSessionFactoryBean;
//    }

//    @Primary
//    @Bean("transactionManager")
//    public PlatformTransactionManager transactionManager(@Qualifier("singleDataSource") DataSource singleDataSource) throws Exception {
//        return new DataSourceTransactionManager(singleDataSource);
//    }

//    @Bean("sessionFactory")
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("singleDataSource") DataSource dataSource) throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(dataSource);
//        sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
//        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
//        sessionFactory.setConfigLocation(new ClassPathResource(this.configLocation));
//        // mapper/*.xml 文件
//        sessionFactory.setMapperLocations(pathMatchingResourcePatternResolver.getResources(this.mapperLocations));
//        return sessionFactory.getObject();
//    }
}
